package com.liu.juc.pool;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 为了让大家更好的理解线程池的概念 ，阿里巴巴JAVA开发规范里面指出，创建线程池需要使用 ThreadPoolExecutor 原生的创建方式
 * 这就涉及到了7大参数
 * int corePoolSize, 核心线程数
 * int maximumPoolSize, 最大线程数
 * long keepAliveTime, 最大等待时间
 * TimeUnit unit, 时间单位
 * BlockingQueue<Runnable> workQueue, 阻塞队列
 * ThreadFactory threadFactory, 线程工厂
 * RejectedExecutionHandler handler 拒绝策略
 */
public class ThreadPoolDemo2 {

    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2, // 核心线程数
                Runtime.getRuntime().availableProcessors(),
                0L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy()
        );
        for (int i = 0; i < 6; i++) {
            threadPoolExecutor.execute(() -> {
                System.out.println(Thread.currentThread().getName() + "--- ok");
            });
        }
        threadPoolExecutor.shutdown();
    }
}
